# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2022/10/20 16:54
desc:
'''
import base.globalvars as glo
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from base.po.mobile_base_page import MobileBasePage
from proj_spec.CLUE.mobile_po.clue_app_base_page import CLUEAppBasePage
from proj_spec.CLUE.mobile_po.company_detail_page import CompanyDetailPage
from proj_spec.CLUE.mobile_po.search_result_page import SearchResultPage


class SearchPage(CLUEAppBasePage):

    #_search_locator = (AppiumBy.CLASS_NAME, "android.widget.EditText")
    # _default_text = '输入股票代码/公司简称搜索'
    # _search_locator = (AppiumBy.XPATH, "//*[contains(@text,text)]/following-sibling::*/android.widget.EditText")
    #_search_locator = (AppiumBy.XPATH, "//*[contains(@text,'输入股票代码/公司简称搜索')]/following-sibling::*/android.widget.EditText")
    _search_locator_android = (AppiumBy.XPATH, "//*[@resource-id='search.search']/*[1]/*[2]/*[1]")
    _search_locator_ios = (AppiumBy.IOS_PREDICATE, 'type == "XCUIElementTypeSearchField"')
    # # 搜索页面覆盖了主页面，先定位上层页面，然后定位可输入的元素
    # _search_locator = (AppiumBy.XPATH, "//*[contains(@text,'pages/index/search')]//*[@focusable='true']")
    _confirm_btn_locator = (AppiumBy.XPATH, "//*[@text='确认']")
    #_back_btn_locator_android =
    _back_btn_locator_ios = (AppiumBy.IOS_CLASS_CHAIN, '**/XCUIElementTypeOther[`label contains "pages/index/search"`]/XCUIElementTypeStaticText[1]') #按钮label为方块，需要添加唯一标识
    # Todo: 搜索页定位
    _search_page_locator_android = (AppiumBy.XPATH, "//*[@resource-id='search.search']")
    _search_locator_ios = (AppiumBy.IOS_PREDICATE, 'type == "XCUIElementTypeSearchField"')


    def search_company(self, keyword):
        # search_box = self.get_searchbox()
        search_box = self.find_element(getattr(self, '_search_locator_' + self.os))
        #search_box.clear() # 搜索一次后text属性变化，无法定位
        if self.os=='android':
            search_box.send_keys(keyword)
        else:
            self.driver.switch_to.active_element.send_keys(keyword)
        return SearchResultPage(self.driver)

    def go_back(self):
        """IOS端driver.back()不work，需要使用后退按钮

        :return:
        """
        if self.os=='android':
            self.driver.back()
        else:
            self.find_element_and_click(getattr(self, '_back_btn_locator_' + self.os))


